package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"teacher-helper/models"
	"teacher-helper/models/request"
	"teacher-helper/service"
)

func GetMessageList(c *gin.Context) {
	var queryInfo request.QueryInfo
	if err := c.ShouldBind(&queryInfo); err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
		return
	}
	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	Messages, total, err := service.GetMessageList(queryInfo, user)
	if err != nil {
		c.JSON(203, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取消息列表成功", "data": map[string]interface{}{
			"messages":Messages,
			"total": total,
		}})
	}
	return
}

func UpdateMessageIsRead(c *gin.Context) {
	var msg models.SysMessage
	if err := c.ShouldBindJSON(&msg); err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
		return
	}
	err := service.UpdateMessageIsRead(msg.ID)
	if err != nil {
		c.JSON(202, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "更新消息状态成功", "data": nil})
	}
	return
}

func GetMessageCount(c *gin.Context) {
	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	msg_total, notice_total, task_total, err := service.GetMessageCount(user)
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取未读消息通知数量成功", "data": map[string]int{
			"msg_total": msg_total,
			"notice_total": notice_total,
			"task_total": task_total,
		}})
	}
	return
}